<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 使用el-tree组件展示主体内容 -->
        <el-tree :data="list" default-expand-all>
          <!-- 使用作用域获取组件内部的数据,可以使用提供的默认的作用域插槽
            ==>{node,data}获取数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- 在插槽内获取list里的id -->
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hedit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="deledit(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 新增部门的弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        title="新增或编辑"
        :visible.sync="dialogVisible"
        @close="closed"
      >
        <!-- v-if 方法1,解决数据不更新  v-if="dialogVisible"-->
        <addDept
          :id="curId"
          ref="myrefADD"
          :origin-list="originList"
          :is-ddit="edit"
          @close="dialogVisible= false"
          @doSuccess="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addDept from './deptDialog.vue'
import { getDepartment, delDepartment } from '@/api/departments.js'
import { render } from '@/utils/index.js'
export default {
  components: {
    addDept
  },
  data() {
    return {
      dialogVisible: false,
      // 依赖一份树形数据
      list: [],
      curId: '',
      edit: false, // false是新增//true是编辑操作,
      originList: []
    }
  },
  created() {
    this.loadgetDepartment()
  },
  methods: {
    closed() {
      this.$refs.myrefADD.resetForm()
    },
    hSuccess() {
      this.loadgetDepartment()
      this.dialogVisible = false
    },
    // 获取树形数据
    async loadgetDepartment() {
      try {
        const res = await getDepartment()
        console.log('getDepartment', res)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        res.data.depts.shift()// 删除公司名称第一条数据
        this.list = render(res.data.depts)
      } catch (err) {
        err => console.log(err)
      }
    },
    // 在子组件实现添加操作将id传送给子级组件
    hadd(id) {
      this.edit = false
      this.dialogVisible = true
      this.curId = id
    },
    // 编制部门
    hedit(id) {
      this.edit = true
      this.dialogVisible = true
      this.curId = id
      // 在子组件实现编辑操作将id传送给子级组件
      // 解决共用组件数据不更新的方式2
      // 可以在父组件中调用子组件中的获取部门详情的数据填充到表单
      /* this.$nextTick(()=>{
          this.$refs.myrefADD.loadDepartmentByid()
        }) */
    },
    deledit(id) {
      this.$confirm('您确定要关吗').then(async() => {
        const res = await delDepartment(id)
        if (!res.code === 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadgetDepartment()
      }).catch((err) => {
        console.log(err)
      })
    }
  }
}
</script>
